<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Education;
use App\Models\Resume;
use App\Models\User;
use DB;
use Illuminate\Http\Request;
use Validator;

class EducationController extends Controller {
	private $role_flag;
	private $user;
	public function __construct() {
		if (\Auth::guard('api')->check()) {
			$getUser = \Auth::guard('api')->user();
			$user = User::find($getUser->id);
			if (!$this->getDevice()) {
				//app
				$user->company_id = $user->appcompany_id;
			}
			$this->user = $user;
			$company = \App\Models\Company::find($this->user->company_id);
			$this->role_flag = $company->role_flag;
		}
	}
	public function get(Request $request, $id) {
		$data = Education::find($id);
		if (!empty($data)) {
			return $this->respond('get_ok', $data);
		}
		return $this->respond('get_error');
	}
	//新增保存
	public function add(Request $request) {
		$user = $this->user;
		$user->guard_name = $this->role_flag;
		$staff = \App\Models\Staff::where(['status' => 0, 'mobile' => $user->mobile, 'role_status' => 1])->first();
		$roleType = $request->input('roleType');
		if ($roleType == 1) {
			if (!empty($staff) && $staff->company_id == $user->company_id) {
				if (!$user->can('resume_edit') && !$user->can('resume_add') && !$user->hasRole('管理员')) {
					return $this->respond('no_auth');
				}
			} else {
				if (!$user->can('resume_edit') && !$user->can('resume_add')) {
					return $this->respond('no_auth');
				}
			}
			return $this->respond('op_ok');
		}
		if (!empty($staff) && $staff->company_id == $user->company_id) {
			if (!$user->can('resume_edit') && !$user->can('resume_add') && !$user->hasRole('管理员')) {
				return $this->respond('no_auth');
			}
		} else {
			if (!$user->can('resume_edit') && !$user->can('resume_add')) {
				return $this->respond('no_auth');
			}
		}
		$validator = Validator::make($request->all(), Education::$rules, Education::$message);
		if ($validator->fails()) {
			return response()->json(['code' => 500, 'msg' => $validator->errors()->first()]);
		}
		$m = new Education();
		$resumeId = $request->input('resumeId');
		if (empty($resumeId)) {
			return response()->json(['code' => 500, 'msg' => '简历id不能为空']);
		}
		$m->resume_id = $resumeId;
		$m->name = $request->input('name');
		$m->major = $request->input('major');
		$m->edu = $request->input('edu');
		$m->start_date = $request->input('startDate');
		$m->end_date = $request->input('endDate');
		$m->experience = $request->input('experience');
		DB::beginTransaction();
		try {
			//中间逻辑代码
			$m->save();
			$get_resume = Education::where(['resume_id' => $resumeId, 'status' => 0])->orderBy('start_date', 'desc')->first();
			Resume::where('id', $resumeId)->update(['edu' => $get_resume->edu]);
			DB::commit();
			return $this->respond('add_ok');
		} catch (\Exception $e) {
			//接收异常处理并回滚
			DB::rollBack();
			dd($e);
			return $this->respond('add_error');
		}
	}
	//更新保存
	public function put(Request $request, $id) {
		$user = $this->user;
		$user->guard_name = $this->role_flag;
		$staff = \App\Models\Staff::where(['status' => 0, 'mobile' => $user->mobile, 'role_status' => 1])->first();
		$roleType = $request->input('roleType');
		if ($roleType == 1) {
			if (!empty($staff) && $staff->company_id == $user->company_id) {
				if (!$user->can('resume_edit') && !$user->hasRole('管理员')) {
					return $this->respond('no_auth');
				}
			} else {
				if (!$user->can('resume_edit')) {
					return $this->respond('no_auth');
				}
			}
			return $this->respond('op_ok');
		}
		if (!empty($staff) && $staff->company_id == $user->company_id) {
			if (!$user->can('resume_edit') && !$user->hasRole('管理员')) {
				return $this->respond('no_auth');
			}
		} else {
			if (!$user->can('resume_edit')) {
				return $this->respond('no_auth');
			}
		}
		if (empty($id)) {
			return response()->json(['code' => 500, 'msg' => '教育经历id不能为空']);
		}
		$validator = Validator::make($request->all(), Education::$rules, Education::$message);
		if ($validator->fails()) {
			return response()->json(['code' => 500, 'msg' => $validator->errors()->first()]);
		}
		$model = new Education();
		$m = $model::find($id);
		if (empty($m)) {
			return $this->respond('get_error');
		}
		$m->name = $request->input('name');
		$m->major = $request->input('major');
		$m->edu = $request->input('edu');
		$m->start_date = $request->input('startDate');
		$m->end_date = $request->input('endDate');
		$m->experience = $request->input('experience');
		$resumeId = $m->resume_id;
		DB::beginTransaction();
		try {
			//中间逻辑代码
			$m->save();
			$get_resume = Education::where(['resume_id' => $resumeId, 'status' => 0])->orderBy('start_date', 'desc')->first();
			Resume::where('id', $resumeId)->update(['edu' => $get_resume->edu]);
			DB::commit();
			return $this->respond('update_ok');
		} catch (\Exception $e) {
			//接收异常处理并回滚
			DB::rollBack();
			return $this->respond('update_error');
		}
	}
	//删除步骤
	public function remove(Request $request, $id) {
		$user = $this->user;
		$user->guard_name = $this->role_flag;
		$staff = \App\Models\Staff::where(['status' => 0, 'mobile' => $user->mobile, 'role_status' => 1])->first();
		if (!empty($staff) && $staff->company_id == $user->company_id) {
			if (!$user->can('resume_edit') && !$user->hasRole('管理员')) {
				return $this->respond('no_auth');
			}
		} else {
			if (!$user->can('resume_edit')) {
				return $this->respond('no_auth');
			}
		}
		if (empty($id)) {
			return response()->json(['code' => 500, 'msg' => '教育经历id不能为空']);
		}
		$m = new Education();
		$model = $m::find($id);
		if (empty($model)) {
			return $this->respond('get_error');
		}
		$model->status = 1;
		DB::beginTransaction();
		$resumeId = $model->resume_id;
		try {
			//中间逻辑代码
			$model->save();
			$get_resume = Education::where(['resume_id' => $resumeId, 'status' => 0])->orderBy('start_date', 'desc')->first();
			$resume_name = $get_resume ? $get_resume->edu : '';
			Resume::where('id', $resumeId)->update(['edu' => $resume_name]);
			DB::commit();
			return $this->respond('delete_ok');
		} catch (\Exception $e) {
			//接收异常处理并回滚
			DB::rollBack();
			return $this->respond('del');
		}
	}
}
